package cn.yr.service.manage.system;

import cn.yr.common.manager.TokenManager;
import cn.yr.common.util.ServletUtils;
import cn.yr.domain.entity.SysUser;
import cn.yr.service.manage.ISysUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
public class TokenService {

	@Value("${token.header}")
	private String header;
	// 令牌秘钥
	@Value("${token.secret}")
	private String secret;

	// 令牌有效期（默认30分钟）
	@Value("${token.expireTime}")
	private int expireTime;

	@Resource
	private ISysUserService userService;

	public String getUserId(){
		HttpServletRequest request = ServletUtils.getRequest();
		String token = request.getHeader(header);
		String id = TokenManager.getUserIdByToken(token);
		return  id;
	}

	public SysUser getUser(){
		String userId = getUserId();
		//TODO 存缓存
		SysUser sysUser = userService.selectUserById(Long.parseLong(userId));
		return sysUser;
	}

}
